package com.powroznik.tracker.engine.util;

import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import javax.ws.rs.Consumes;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyReader;
import javax.ws.rs.ext.Provider;

import com.powroznik.tracker.engine.data.DeviceConfiguration;

@Consumes("application/json")
@Provider
public class DeviceConfigurationMessageBodyReader implements
		MessageBodyReader<DeviceConfiguration> {
	@Override
	public boolean isReadable(Class<?> c, Type t, Annotation[] as, MediaType mt) {
		return DeviceConfiguration.class.isAssignableFrom(c);
	}

	@Override
	public DeviceConfiguration readFrom(Class<DeviceConfiguration> c, Type t,
			Annotation[] a, MediaType mt, MultivaluedMap<String, String> mm,
			InputStream is) throws IOException, WebApplicationException {
		return ConversionUtils.readDeviceConfiguration(ConversionUtils
				.readString(is));
	}
}
